﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ComeSeeThis_MVC2.Models;

namespace ComeSeeThis_MVC2.Controllers
{
    public class GearingRecController : Controller
    {
        readonly GearingRecRepository _gearingRecRepository = new GearingRecRepository();

        #region Queries

        public ActionResult Index()
        {
            var gearingRec = new GearingRec();
            return View(new GearingRecSearchForm(gearingRec));
        }

        [HttpPost]
        public ActionResult Index(GearingRec gearingRec, int cellCount, float transmissionRatio)
        {
            return View("SearchResults", new GearingRecSearchResultForm(gearingRec, cellCount, transmissionRatio));
            //return RedirectToAction("SearchResults", new {gearingRec, cellCount, transmissionRatio});
        }

        public ActionResult SearchResults(GearingRec gearingRec, int cellCount, float transmissionRatio)
        {
            return View(new GearingRecSearchResultForm(gearingRec, cellCount, transmissionRatio));
        }

        public ActionResult Browse()
        {
            var gearingRecs = new GearingRecRepository().GetAllGearingRecs();
            return View(gearingRecs);
        }

        #endregion

        #region User Submit

        //[Authorize] //(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Submit()
        {
            var gearingRec = new GearingRec();
            return View(new GearingRecFormView(gearingRec));
        }

        [HttpPost]//, Authorize] //(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Submit(GearingRec gearingRec, int cellCount)
        {
            if (ModelState.IsValid)
            {
                gearingRec.DateSubmitted = DateTime.Now;
                gearingRec.SubmittedBy = User.Identity.Name;
                gearingRec.CellCount = cellCount;
                _gearingRecRepository.Add(gearingRec);
                _gearingRecRepository.Save();

                return RedirectToAction("Browse");
            }
            return View(new GearingRecFormView(gearingRec));
        }

        #endregion
    }
}
